<?php

declare(strict_types=1);

namespace App\Admin;

use App\Entity\AdminUser;
use App\Entity\Blogroll;
use App\Form\Type\BlogrollLogoFieldType;
use App\Utils\Helper\GetterHelper;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Route\RouteCollection;
use Sonata\AdminBundle\Show\ShowMapper;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Security\Core\Security;

final class BlogrollAdmin extends AbstractAdmin {
    protected $baseRouteName = 'admin_blogroll';
    protected $baseRoutePattern = 'blogroll';
    protected $maxPageLinks = 10;
    protected $security;

    public function __construct ($code, $class, $baseControllerName, Security $security) {
        parent::__construct($code, $class, $baseControllerName);
        $this->security = $security;
    }

    public function toString ($object) {
        return $object instanceof Blogroll ? $object->getName() : '友链';
    }

    protected function configureDefaultSortValues (array &$sortValues) {
        $sortValues['_page'] = 1;
        $sortValues['_per_page'] = 32;
        $sortValues['_sort_order'] = 'DESC';
        $sortValues['_sort_by'] = 'id';
    }

    protected function configureDatagridFilters (DatagridMapper $datagridMapper): void {
        $datagridMapper
            ->add('name', null, ['label' => '名称'])
            ->add('url', null, ['label' => '链接']);
    }

    protected function configureListFields (ListMapper $listMapper): void {
        $listMapper
            ->addIdentifier('name', null, [
                'label'        => '名称',
                'header_style' => 'width: 35%; text-align: left',
                'row_align'    => 'left',
            ])
            ->add('url', null, [
                'label'        => '链接',
                'header_style' => 'width: 25%; text-align: left',
                'row_align'    => 'left',
            ])
            ->add('admin_user_id', null, [
                'label'        => '创建人',
                'admin_users'  => GetterHelper::getEntityManager()->getRepository(AdminUser::class)->findAllToArray(),
                'template'     => 'backend/public/list_admin_user_id.html.twig',
                'header_style' => 'width: 15%; text-align: left',
                'row_align'    => 'left',
            ])
            ->add('created_at', null, [
                'label'        => '创建时间',
                'format'       => 'Y-m-d H:i:s',
                'timezone'     => 'PRC',
                'header_style' => 'width: 15%; text-align: left',
                'row_align'    => 'left',
            ]);
    }

    protected function configureFormFields (FormMapper $formMapper): void {
        $formMapper
            ->add('logo', HiddenType::class)
            ->add('logo_item', BlogrollLogoFieldType::class, [
                'label' => 'Logo',
            ])
            ->add('name', null, [
                'label'    => '名称',
                'required' => true,
            ])
            ->add('url', null, [
                'label'    => '链接',
                'required' => true,
            ]);
    }

    protected function configureShowFields (ShowMapper $showMapper): void {
        $showMapper
            ->add('id')
            ->add('logo')
            ->add('name')
            ->add('url')
            ->add('admin_user_id')
            ->add('created_at')
            ->add('updated_at');
    }

    public function getTemplate ($name) {
        switch ($name) {
            case 'edit':
                return 'backend/blogroll/edit.html.twig';
            default:
                return $this->getTemplateRegistry()->getTemplate($name);
        }
    }

    protected function configureRoutes (RouteCollection $collection) {
        parent::configureRoutes($collection); // TODO: Change the autogenerated stub
        $collection->remove('export');
        $collection->remove('show');
    }

    public function prePersist ($object) {
        parent::prePersist($object); // TODO: Change the autogenerated stub

        $object->setAdminUserId($this->security->getUser()->getId());
        $object->setCreatedAt(new \DateTime());
    }
}
